package com.tudouji.project.mt.service.impl;

import java.util.Date;
import java.util.List;

import com.tudouji.common.exception.CustomException;
import com.tudouji.common.utils.DateUtils;
import com.tudouji.project.mt.domain.MtShop;
import com.tudouji.project.mt.domain.MtToken;
import com.tudouji.project.mt.service.IMtShopService;
import com.tudouji.project.mt.service.IMtTokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tudouji.project.mt.mapper.MtFoodcostcardMapper;
import com.tudouji.project.mt.domain.MtFoodcostcard;
import com.tudouji.project.mt.service.IMtFoodcostcardService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import static com.tudouji.project.mt.domain.MtShop.cost_state;
import static com.tudouji.project.mt.domain.MtShop.whout_state;

/**
 * 美团成本卡主Service业务层处理
 * 
 * @author tudouji
 * @date 2023-12-04
 */
@Slf4j
@Service
public class MtFoodcostcardServiceImpl implements IMtFoodcostcardService 
{
    @Autowired
    private MtFoodcostcardMapper mtFoodcostcardMapper;
    @Autowired
    private IMtShopService mtShopService;
    @Autowired
    private MtPullService mtPullService;
    @Autowired
    private IMtTokenService iMtTokenService;

    /**
     * 查询美团成本卡主
     * 
     * @param id 美团成本卡主ID
     * @return 美团成本卡主
     */
    @Override
    public MtFoodcostcard selectMtFoodcostcardById(Long id)
    {
        return mtFoodcostcardMapper.selectMtFoodcostcardById(id);
    }

    /**
     * 查询美团成本卡主列表
     * 
     * @param mtFoodcostcard 美团成本卡主
     * @return 美团成本卡主
     */
    @Override
    public List<MtFoodcostcard> selectMtFoodcostcardList(MtFoodcostcard mtFoodcostcard)
    {
        return mtFoodcostcardMapper.selectMtFoodcostcardList(mtFoodcostcard);
    }

    /**
     * 新增美团成本卡主
     * 
     * @param mtFoodcostcard 美团成本卡主
     * @return 结果
     */
    @Override
    public int insertMtFoodcostcard(MtFoodcostcard mtFoodcostcard)
    {
        mtFoodcostcard.setCreateTime(DateUtils.getNowDate());
        return mtFoodcostcardMapper.insertMtFoodcostcard(mtFoodcostcard);
    }

    /**
     * 修改美团成本卡主
     * 
     * @param mtFoodcostcard 美团成本卡主
     * @return 结果
     */
    @Override
    public int updateMtFoodcostcard(MtFoodcostcard mtFoodcostcard)
    {
        mtFoodcostcard.setUpdateTime(DateUtils.getNowDate());
        return mtFoodcostcardMapper.updateMtFoodcostcard(mtFoodcostcard);
    }

    /**
     * 批量删除美团成本卡主
     * 
     * @param ids 需要删除的美团成本卡主ID
     * @return 结果
     */
    @Override
    public int deleteMtFoodcostcardByIds(Long[] ids)
    {
        return mtFoodcostcardMapper.deleteMtFoodcostcardByIds(ids);
    }

    /**
     * 删除美团成本卡主信息
     * 
     * @param id 美团成本卡主ID
     * @return 结果
     */
    @Override
    public int deleteMtFoodcostcardById(Long id)
    {
        return mtFoodcostcardMapper.deleteMtFoodcostcardById(id);
    }

    @Override
    @Transactional
    public void dataPull(List<MtShop> mtShopList) {
        log.info("消耗单拉取"+this.getClass() + DateUtils.getTime());
//        Date yestoday = DateUtils.addDays(DateUtils.getNowDate(), -1);
        MtToken mtToken = iMtTokenService.getLastTokenByConditions(new MtToken());

        for (MtShop shop : mtShopList) {
            //获取当前店铺与昨天的天数差
//            long dateDiff = DateUtils.getDateDiff(yestoday, shop.getPulldate());
//            for (int i = 0; i < dateDiff; i++) {

            try {
                dataPullWithShop(mtToken, shop);
            } catch (Exception e) {
                throw new CustomException(e.getMessage());
            }
//            }
        }
        log.info("消耗单拉取"+this.getClass() + DateUtils.getTime());
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public void dataPullWithShop(MtToken mtToken, MtShop shop)  throws Exception {
        Date pulldate = shop.getPulldate();
        Long timeBegin = pulldate.getTime();
        Long timeEnd = DateUtils.getLastDayOfDate(pulldate).getTime();

        //获取MT接口数据
        mtPullService.getCost(shop, timeBegin, timeEnd, mtToken);
        //转换为业务实体，并插入,如果已存在对应ID的实体，则跳过

        shop = mtShopService.selectMtShopById(shop.getId());
        if (!shop.isPullFinished(cost_state)){
            shop.setPullState(cost_state, 1);
        }
        mtShopService.checkAndInitPullDate(shop);
        mtShopService.updateMtShop(shop);
    }
}
